<template>
  <a-modal
    title="壁纸"
    :width="1000"
    :visible="visible"
    :confirm-loading="confirmLoading"
    cancel-text="关闭"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="壁纸类型"
        >
          <ants-dict-select-tag
            v-decorator="['fileType', validatorRules.fileType]"
            type="radioButton"
            :trigger-change="true"
            dict-code="wallpaper_type"
            placeholder="请选择壁纸类型"
          />
        </a-form-item>

        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="壁纸上传"
        >
          <div style="color: red">一次最多上传10张！！！就算你选的再多，我也不给你上传！！！哈哈哈哈哈哈</div>
          <ants-image-upload
            v-decorator="['fileName', validatorRules.fileName]"
            :number="10"
            biz-path="ants-wallpaper"
            is-multiple
          />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { httpAction, postAction } from '@/api/manage'
import AntsImageUpload from '@/components/ants/AntsImageUpload'
import AntsDictSelectTag from '@/components/dict/AntsDictSelectTag'

export default {
  name: 'WallpaperModal',
  components: {
    AntsDictSelectTag,
    AntsImageUpload
  },
  data() {
    return {
      form: this.$form.createForm(this),
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      confirmLoading: false,
      validatorRules: {
        fileName: { rules: [{ required: true, message: '请上传壁纸!' }] },
        fileType: { rules: [{ required: true, message: '请选择壁纸类型!' }] }
      },
      url: {
        add: '/wallpaper/add'
      }
    }
  },
  created() {
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'dictName', 'dictCode', 'description'))
      })
    },
    // 确定
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          const formData = Object.assign(this.model, values)
          httpAction(that.url.add, 'post', formData).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
              this.close()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
  }
}
</script>
